package product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import product.entity.Product;
import product.mapper.ProductMapper;
import product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lh
 * @since 2020-08-08
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Override
    public boolean deduct(String productCode, Integer count) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product::getProductCode, productCode);
        Product product = getOne(queryWrapper);
        if (product.getCount() - count < 0) {
            throw new RuntimeException("余量不足");
        }
        product.setCount(product.getCount() - count);
        return updateById(product);
    }
}
